package kt.com.bilin.coin.ui.activity

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemRechargeMoneyBinding
import kotlinx.android.synthetic.main.activity_prepaid_refill.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.RechargeInfoBean
import kt.com.bilin.coin.bean.RechargeMoneyBean
import kt.com.bilin.coin.common.*
import kt.com.bilin.coin.ui.adapter.BaseSignCheckAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.ui.dialog.InputDialog
import kt.com.bilin.coin.ui.dialog.RechargeConfirmDialog
import kt.com.bilin.coin.ui.widget.CommonButton
import kt.com.bilin.coin.utils.FullyGridLayoutManager

@Route(path = PATH_PREPAID_REFILL)
class PrepaidRefillActivity : BaseActivity(R.layout.activity_prepaid_refill) {
    private val mRechargeMoneyAdapter: RechargeMoneyAdapter = RechargeMoneyAdapter()
    override fun initView() {
        bindViewClick(this, buttonRechargeNow, titleView.rightTextView)
        rvMoney.layoutManager = FullyGridLayoutManager(this, 3)
        (rvMoney.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        rvMoney.adapter = mRechargeMoneyAdapter
        etPhone.setText(UserDataCache.userInfo.mobile)
        refreshLayout.setOnRefreshListener { onReload() }
        refreshLayout.setEnableLoadMore(false)
    }

    override fun initData() {
        onReload()
    }

    private fun onReload() {
        tvJdc.gone()
        launchUI({ API_SERVICE.priceList().toData() }, ApiObserver(refreshLayout = refreshLayout) {
            mRechargeMoneyAdapter.datas.clear()
            it?.forEach {
                mRechargeMoneyAdapter.datas.add(RechargeMoneyBean(it))
            }
            mRechargeMoneyAdapter.notifyDataSetChanged()
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            buttonRechargeNow -> {
                val mRechargeMoneyBean = mRechargeMoneyAdapter.getCheck()
                if (mRechargeMoneyBean.jdc.isEmpty()) {
                    "请先选择充值金额".toastMessageLong()
                    return
                }
                RechargeConfirmDialog(this, RechargeInfoBean(mRechargeMoneyBean.money, mRechargeMoneyBean.jdc, etPhone.text.toString())) {
                    launchUI({ API_SERVICE.telRecharge(it.money, it.phone).toData() }, ApiObserver(this, commonButton = buttonRechargeNow) {
                        "充值成功".toastMessageLong()
                        onReload()
                    })
                }.show()
//                val mRechargeMoneyBean = mRechargeMoneyAdapter.getCheck()
//                launchUI({ API_SERVICE.recharge(mRechargeMoneyBean.money, 1, etPhone.text.toString()).toData() }, ApiObserver(this, commonButton = buttonRechargeNow) {
//                    "充值成功".toastMessageLong()
//                })
            }
            titleView.rightTextView -> ARouterNavigation(PATH_RECHARGE_RECORD)
        }
    }

    inner class RechargeMoneyAdapter : BaseSignCheckAdapter<BaseViewHolder<ItemRechargeMoneyBinding>, RechargeMoneyBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemRechargeMoneyBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemRechargeMoneyBinding>, bean: RechargeMoneyBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    tvMoney.text = "${money}元"
                    tvMoney.changeStyle(if (check) CommonButton.CB_NORMAL else CommonButton.CB_GREY)
                    setViewClick(position, tvMoney)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            super.subOnClickAdapter(v, position)
            val mRechargeMoneyBean = getItem(position)
            launchUI({ API_SERVICE.countFee(mRechargeMoneyBean.money).toData() }, ApiObserver(this@PrepaidRefillActivity) {
                it?.run {
                    mRechargeMoneyBean.jdc = principal
                    tvJdc.visible()
                    tvJdc.text = "≈${principal}JDC  手续费：${fee}JDC"
                }
            })
        }
    }
}